package com.an.pay.service.Impl;

import com.an.pay.client.AliPayClient;
import com.an.pay.entity.AliRefundParams;
import com.an.pay.entity.OrderInfo;
import com.an.pay.entity.RefundInfo;
import com.an.pay.enums.OrderStatus;
import com.an.pay.mapper.RefundInfoMapper;
import com.an.pay.service.OrderInfoService;
import com.an.pay.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class RefundServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private AliPayClient aliPayClient;

    @Transactional
    @Override
    public void refund(String orderNo, String reason, String type) {
        // 第一步，查询订单
        OrderInfo orderInfo = orderInfoService.getOne(new QueryWrapper<OrderInfo>().eq("order_no", orderNo));
        if (orderInfo == null) {
            throw new RuntimeException("没有【" + orderNo + "】的订单");
        }
        //第2个步，创建退款订单
        RefundInfo refundOrder = createRefundOrder(orderInfo, reason);
        baseMapper.insert(refundOrder);
        //构建退款参数
        AliRefundParams aliRefundParams = AliRefundParams.builder()
                .refundReason(reason) // 退款信息
                .refundAmount(orderInfo.getTotalFee()) //退款金额
                .outTradeNo(orderNo)
                .build();
        String refund = aliPayClient.refund(aliRefundParams);
        // 把申请退款的返回参数和refund_id保存进数据库
        Gson gson = new Gson();
        Map map = gson.fromJson(refund, Map.class);
        Map<String, String> data = (Map<String, String>) map.get("alipay_trade_refund_response");
        if(!data.get("code").equals("10000")){
            // 退款异常
            // 更新订单
            OrderInfo order = new OrderInfo();
            order.setOrderStatus(OrderStatus.REFUND_ABNORMAL.getType());
            orderInfoService.update(order,new QueryWrapper<OrderInfo>().eq("order_no",orderNo));
            // 更新退款订单
            RefundInfo refundInfo = new RefundInfo();
            refundInfo.setRefundStatus(OrderStatus.REFUND_ABNORMAL.getType());
            baseMapper.update(refundInfo,new QueryWrapper<RefundInfo>().eq("order_no",orderNo));
        }else {
            // 退款成功
            String refundId = data.get("buyer_user_id");
            //拿到数据保存进数据库
            // 更新支付订单
            OrderInfo order = new OrderInfo();
            order.setOrderStatus(OrderStatus.REFUND_SUCCESS.getType());
            orderInfoService.update(order,new QueryWrapper<OrderInfo>().eq("order_no",orderNo));
            // 更新退款订单
            RefundInfo refundInfo = new RefundInfo();
            refundInfo.setRefundId(refundId);
            refundInfo.setContentReturn(refund);
            refundInfo.setRefundStatus(OrderStatus.REFUND_SUCCESS.getType());
            baseMapper.update(refundInfo,new QueryWrapper<RefundInfo>().eq("order_no",orderNo));
        }


    }

    private RefundInfo createRefundOrder(OrderInfo orderInfo, String reason) {
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setRefundNo(UUID.randomUUID().toString().substring(10));
        refundInfo.setOrderNo(orderInfo.getOrderNo());
        refundInfo.setReason(reason);
        refundInfo.setRefundStatus(OrderStatus.REFUND_PROCESSING.getType());
        refundInfo.setRefund(orderInfo.getTotalFee());
        refundInfo.setTotalFee(orderInfo.getTotalFee());
        return refundInfo;
    }


}
